package idz.jsf;

import java.io.Serializable;
import java.util.LinkedList;

import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;

@ManagedBean(name = "dataBean", eager = true)
@ApplicationScoped
public class DataBean implements Serializable
{
	private static final long serialVersionUID = 5812904811038924657L;
	private LinkedList<User> users = new LinkedList<User> ();
	
	public void registerUser (User user)
	{
		users.add (user);
		System.out.println (users.get(0).getNick());
	}
	
	public boolean loginUser (String nick, String pass)
	{
		for (int i = 0; i < users.size (); i++)
		{
			System.out.println (users.get(i).getNick());
			if (nick.equals (users.get(i).getNick ()) && pass.equals(users.get(i).getPassword ()))
				return true;			
		}
		return false;	
	}
	
	public boolean checkLogin (String nick)
	{
		for (User u : users)
		{
			if (nick.equals (u.getNick ()))
				return false;			
		}
		return true;		
	}
	
	public void redirect (final String url)
	{
		try
		{
			FacesContext.getCurrentInstance().getExternalContext().redirect(url);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}
}
